package com.example.administrator.smartdevice.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.smartdevice.R;
import com.example.administrator.smartdevice.event.MessageEvent;
import com.example.administrator.smartdevice.utils.StringUtils;

import java.util.List;

/**
 * Created by Administrator on 2018/4/19.
 */

public class AskListAdapter extends RecyclerView.Adapter<AskListAdapter.ViewHolder> {
    private Context mContext;
    private List<MessageEvent> data;

    public AskListAdapter(Context mContext, List<MessageEvent> data) {
        this.mContext = mContext;
        this.data = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_ask_list_item_add_device, parent, false);
        ViewHolder mViewHolder = new ViewHolder(view);
        return mViewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.fragment_ask_list_item_add_device_img_icon.setImageResource(R.mipmap.ic_launcher);
        holder.fragment_ask_list_item_add_device_tv_type.setText(StringUtils.getApplianceType(data.get(position).getApplianceType()));
        holder.fragment_ask_list_item_add_device_tv_appliance_id.setText(data.get(position).getApplianceId());
        holder.btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public List<MessageEvent> getDataList() {
        return data;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView fragment_ask_list_item_add_device_img_icon;
        private TextView fragment_ask_list_item_add_device_tv_appliance_id;
        private TextView fragment_ask_list_item_add_device_tv_type;
        private RelativeLayout fragment_ask_list_item_add_device_item;
        private Button btnAdd;

        public ViewHolder(View itemView) {
            super(itemView);
            fragment_ask_list_item_add_device_img_icon = (ImageView) itemView.findViewById(R.id.fragment_ask_list_item_add_device_img_icon);
            fragment_ask_list_item_add_device_tv_appliance_id = (TextView) itemView.findViewById(R.id.fragment_ask_list_item_add_device_tv_appliance_id);
            fragment_ask_list_item_add_device_tv_type = (TextView) itemView.findViewById(R.id.fragment_ask_list_item_add_device_tv_type);
            fragment_ask_list_item_add_device_item = (RelativeLayout) itemView.findViewById(R.id.fragment_ask_list_item_add_device_item);
            btnAdd = (Button) itemView.findViewById(R.id.btnAdd);
        }
    }

    public void setNewData(List<MessageEvent> list) {
        data.clear();
        data.addAll(list);
        notifyDataSetChanged();
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
